package com.yonyou.pmclouds.plantemplate.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateItemVO;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateVO;
import com.yonyou.pmclouds.plantemplate.mapper.ScheduleTemplateMapper;
import com.yonyou.pmclouds.plantemplate.rmiitf.IScheduleTemplateQueryService;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = IScheduleTemplateQueryService.class)
public class ScheduleTemplateQueryServiceImpl implements IScheduleTemplateQueryService {

    @Autowired
    private ScheduleTemplateMapper templateMapper;

    @Autowired
    private ScheduleTemplateItemQueryServiceImpl itemQueryService;

    @Autowired
    private OrganizationQuery organizationQuery;

    @Autowired
    private ProjectTypeQuery projectTypeQuery;

    @Override
    public ScheduleTemplateVO queryById(String pkScheduleTemplate) throws BusinessException {
        ScheduleTemplateVO templateVO = templateMapper.getById(pkScheduleTemplate);

        if (templateVO == null) {
            return new ScheduleTemplateVO();
        }

        setItemVOS(new ScheduleTemplateVO[]{templateVO});

        return templateVO;
    }

    @Override
    public ScheduleTemplateVO[] searchTemplate(String keyword) throws BusinessException {
        ScheduleTemplateVO[] templateVOS = templateMapper.searchTemplate(RuntimeEnvironment.getTenantId(), keyword);

        if(templateVOS == null || templateVOS.length <= 0){
            return new ScheduleTemplateVO[0];
        }

        setItemVOS(templateVOS);

        return templateVOS;
    }

    @Override
    public ScheduleTemplateVO[] autoSearchPlanTemp(String pkTenant, String pkOrg, String type) throws BusinessException  {
        ScheduleTemplateVO[] templateVOS = templateMapper.autoSearchPlanTemp(pkTenant,pkOrg,type);

        if(templateVOS == null || templateVOS.length <= 0){
            return new ScheduleTemplateVO[0];
        }

        setItemVOS(templateVOS);

        return templateVOS;
    }

    private void setItemVOS(ScheduleTemplateVO[] templateVOS) throws BusinessException {
        if (templateVOS == null || templateVOS.length <= 0) {
            return;
        }

        List<String> pkTemplateList = new ArrayList<>(templateVOS.length);
        Set<String> pkOrgSet = new HashSet<>();
        Set<String> typeSet = new HashSet<>();

        for (ScheduleTemplateVO templateVO : templateVOS) {
            pkOrgSet.add(templateVO.getPkOrg());
            typeSet.add(templateVO.getType());
            pkTemplateList.add(templateVO.getPkScheduleTemplate());
        }

        // 获取所有的计划节点
        Map<String, List<ScheduleTemplateItemVO>> itemMap = itemQueryService.queryByPkSchedulesOnlyRoot(
                pkTemplateList.toArray(new String[]{}));

        // 查询组织名称
        Map<String, OrganizationVO> organizationNameMap = queryOrganizationName(pkOrgSet);

        // 查询计划类型名称
        Map<String, String> typeNameMap = projectTypeQuery.queryNamesByIds(typeSet.toArray(new String[0]));

        for (ScheduleTemplateVO templateVO : templateVOS) {
            String typeName = typeNameMap.get(templateVO.getType());
            if(StringUtils.isNotEmpty(typeName)){
                templateVO.setTypeName(typeName);
            }

            OrganizationVO organizationVO = organizationNameMap.get(templateVO.getPkOrg());
            if(organizationVO != null){
                templateVO.setOrgName(organizationVO.getSname());
            }

            // 添加计划任务项
            List<ScheduleTemplateItemVO> itemVOS = itemMap.get(templateVO.getPkScheduleTemplate());
            if (itemVOS == null || itemVOS.size() <= 0) {
                continue;
            }

            templateVO.setScheduleTemplateItemVOS(itemVOS.toArray(new ScheduleTemplateItemVO[]{}));

        }
    }

    /**
     * 查询组织名称
     */
    private Map<String, OrganizationVO> queryOrganizationName(Set<String> pkOrgSet) throws BusinessException {
        Map<String, OrganizationVO> result = new HashMap<>();
        if (CollectionUtils.isEmpty(pkOrgSet))
            return result;

        OrganizationVO[] organizationVOs = organizationQuery.queryByIds(pkOrgSet.toArray(new String[0]));
        if (ArrayUtils.isEmpty(organizationVOs))
            return result;

        for (OrganizationVO vo : organizationVOs) {
            result.put(vo.getPkOrganization(), vo);
        }

        return result;
    }
}
